package cn.itcast.processDefined;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.io.FileUtils;
import org.junit.Test;


public class ProcessDefinationTest {
	ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();

	@Test
	public void deploymentProcessDefinition(){
	Deployment deployment	=processEngine.getRepositoryService()//与流程定义和部署对象关联的
		            .createDeployment()//创建部署对象
		            .name("流程定义")
		            .addClasspathResource("diagrams/Hello.bpmn")
		            .addClasspathResource("diagrams/Hello.png")//一次加载一个文件
		            .deploy();//完成部署
	System.out.println(deployment.getId());
	System.out.println(deployment.getName());
	}
	@Test
	public void deploymentProcessDefinition_zip(){
	InputStream in=this.getClass().getClassLoader().getResourceAsStream("diagrams/hello.zip");
	ZipInputStream zipInputStream =new ZipInputStream(in);
	Deployment deployment	=processEngine.getRepositoryService()//与流程定义和部署对象关联的
		            .createDeployment()//创建部署对象
		            .name("流程定义")
		            .addZipInputStream(zipInputStream)
		            .deploy();//完成部署
	System.out.println(deployment.getId());
	System.out.println(deployment.getName());
	}
	//查询流程定义
	@Test
	public void findProcessDefinition(){
	List<ProcessDefinition> list=	processEngine.getRepositoryService()
		             .createProcessDefinitionQuery()
		            // .deploymentId(deploymentId)使用部署对象id查询
		            // .processDefinitionId(processDefinitionId)使用流程定义id查询
		            //.processDefinitionNameLike(processDefinitionNameLike)使用流程定义名字模糊查询
		            .list();
		         //   .listPage(firstResult, maxResults) //分页查询;
	if(list!=null&&list.size()>=0){
		for(ProcessDefinition pd:list){
			System.out.println("流程定义id"+pd.getId());
			System.out.println("流程定义名称"+pd.getName());
			System.out.println("流程定义kay"+pd.getKey());
			System.out.println("资源名称bpmn"+pd.getResourceName());
			System.out.println("资源名称png"+pd.getDiagramResourceName());
			System.out.println("部署对象id"+pd.getDeploymentId());
			System.out.println("*************************************");
		}
	}
	}
	
	//删除流程定义
	@Test
	public void deleteProcessDefinition(){
		processEngine.getRepositoryService()
		              .deleteDeployment("4001", true);
		System.out.println("删除流程定义");
		
	}	
	//查看流程图
	
	@Test
	public void viewPic(){
		//将生成图片放到文件夹下
		//获取图片资源名称  
	List<String>  list=	 processEngine.getRepositoryService()
		            .getDeploymentResourceNames("301");
	
	  String resourcename="";
	  if(list!=null&& list.size()>=0){
		  for(String name:list){
			  if(name.indexOf(".png")>=0){
				  resourcename=name;
			  }
		  }
	  }
	  //获取图片输入流
	  InputStream in=processEngine.getRepositoryService()
			                      .getResourceAsStream("301", resourcename);
	  
		File file=new File("D:/"+resourcename);
		//讲输入流图片写到d盘下
		try {
			FileUtils.copyInputStreamToFile(in, file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
	}
	//查询最新版本的流程定义
	@Test
    public void findLastVersonProcessDefinition(){
    	List<ProcessDefinition> list=processEngine.getRepositoryService()
                                                   .createProcessDefinitionQuery()
                                                   .orderByProcessDefinitionVersion().asc()//使用流程定义版本升序排列
                                                   .list();
    	//map中的key：流程定义的key
    	//map中的value：流程定义的对象
    	//map集合特点：当map集合的key值相同的情况下，后一次的值替换前一次的值
         Map<String,ProcessDefinition> map=new LinkedHashMap<String, ProcessDefinition>();
         if(list!=null&&list.size()>0){
        	  for(ProcessDefinition pd:list){
        		  map.put(pd.getKey(), pd);
        	  }
         }
         List<ProcessDefinition> pdlist=new ArrayList<ProcessDefinition>(map.values());
         if(pdlist!=null&&pdlist.size()>=0){
     		for(ProcessDefinition pd:pdlist){
     			System.out.println("流程定义id"+pd.getId());
     			System.out.println("流程定义名称"+pd.getName());
     			System.out.println("流程定义kay"+pd.getKey());
     			System.out.println("资源名称bpmn"+pd.getResourceName());
     			System.out.println("资源名称png"+pd.getDiagramResourceName());
     			System.out.println("部署对象id"+pd.getDeploymentId());
     			System.out.println("*************************************");
     		}
    }
         //`act_re_procdef`流程定义表
         //`act_re_deployment`部署对象表
         //`act_ge_property`主键生成策略表
         //`act_ge_bytearray``资源文件表
}
	//附加功能：删除流程定义（删除key相同的所有不同版本的流程定义）
	@Test
	public void deleteProcessDefinitingByKey(){
		String key="hello";
		//先使用流程定义的key查询流程定义，查出所有的版本
		List<ProcessDefinition> list=processEngine.getRepositoryService()
				                     .createProcessDefinitionQuery()
				                     .processDefinitionKey(key)
				                     .list();
		//遍历获取每个流程定义的部署id
		if(list!=null&&list.size()>0){
			//获取部署id
			for(ProcessDefinition pd:list){
				String deploymentId=pd.getDeploymentId();
				processEngine.getRepositoryService()
				             .deleteDeployment(deploymentId,true);
				
			}
			
		}
	}
}